package com.gin.no_gay_alliance.param;

import android.content.Context;
import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;

import com.gin.no_gay_alliance.activity.BaseBindingActivity;
import com.gin.no_gay_alliance.activity.TopicReadActivity;
import com.gin.no_gay_alliance.utils.UrlUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 阅读主题/回复 参数
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/14 14:44
 **/
public class TopicReadParam implements Parcelable {
    public static final Creator<TopicReadParam> CREATOR = new Creator<TopicReadParam>() {
        @Override
        public TopicReadParam createFromParcel(Parcel in) {
            return new TopicReadParam(in);
        }

        @Override
        public TopicReadParam[] newArray(int size) {
            return new TopicReadParam[size];
        }
    };
    /**
     * 主题ID
     */
    public Long topicId;
    /**
     * 回复ID ，用于请求单个回复或跳转到指定回复
     */
    public Long replyId;
    /**
     * 页码
     */
    public Integer page;
    /**
     * 作者uid(只看TA功能)
     */
    public Long authorUid;
    /**
     * 楼层号(用于跳转)
     */
    public Integer level;
    /**
     * 总页数(临时)
     */
    public Integer totalPage;
    /**
     * 总楼层数(外部传入,用以判断最新一页应当有多少个回复)
     */
    public Integer total;
    /**
     * 目标楼层, 用于滚动
     */
    public Integer targetLevel;
    /**
     * 编辑历史备份id
     */
    public Long backupId;

    public TopicReadParam() {
    }

    protected TopicReadParam(Parcel in) {
        if (in.readByte() == 0) {
            topicId = null;
        } else {
            topicId = in.readLong();
        }
        if (in.readByte() == 0) {
            replyId = null;
        } else {
            replyId = in.readLong();
        }
        if (in.readByte() == 0) {
            page = null;
        } else {
            page = in.readInt();
        }
        if (in.readByte() == 0) {
            authorUid = null;
        } else {
            authorUid = in.readLong();
        }
        if (in.readByte() == 0) {
            level = null;
        } else {
            level = in.readInt();
        }
        if (in.readByte() == 0) {
            totalPage = null;
        } else {
            totalPage = in.readInt();
        }
        if (in.readByte() == 0) {
            total = null;
        } else {
            total = in.readInt();
        }
        if (in.readByte() == 0) {
            targetLevel = null;
        } else {
            targetLevel = in.readInt();
        }
        if (in.readByte() == 0) {
            backupId = null;
        } else {
            backupId = in.readLong();
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (topicId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(topicId);
        }
        if (replyId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(replyId);
        }
        if (page == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(page);
        }
        if (authorUid == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(authorUid);
        }
        if (level == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(level);
        }
        if (totalPage == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(totalPage);
        }
        if (total == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(total);
        }
        if (targetLevel == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(targetLevel);
        }
        if (backupId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(backupId);
        }
    }

    /**
     * 使用当前参数启动活动
     *
     * @param context 上下文
     */
    public void startActivity(Context context, String title) {
        Intent intent = new Intent(context, TopicReadActivity.class);
        intent.putExtra(TopicReadParam.class.getName(), this);
        intent.putExtra(BaseBindingActivity.INTENT_KEY_TITLE, title);
        context.startActivity(intent);
    }

    /**
     * 从url中解析参数
     *
     * @param url url
     * @return 参数
     */
    public static TopicReadParam parseUrl(String url) {
        TopicReadParam param = new TopicReadParam();
        HashMap<String, List<String>> queryParams = UrlUtils.parseQuery(url);
        for (Map.Entry<String, List<String>> entry : queryParams.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue().get(0);
            switch (key) {
                case "page":
                    param.page = Integer.valueOf(value);
                    break;
                case "authorid":
                    param.authorUid = Long.valueOf(value);
                    break;
                case "tid":
                    param.topicId = Long.valueOf(value);
                    break;
                case "pid":
                    param.replyId = Long.valueOf(value);
                    break;
            }
        }
        return param;
    }
}
